/**
 * Copyright (c) 2024 - CARThunder
 * All Rights Reserved by Chengdu CARThunder Intelligent Technology Co., LTD and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with CARThunder in writing by applicable law.
 * <p>
 * <p>
 * file description.
 *
 * @file filename
 * @brief brief function description.
 * @details detailed function description.
 * @version 1.0
 * @author author_name
 * @date last_modified_date
 * <p>
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2024-08-2               jia fengyi         Create it.
 */

/**
 * file description.
 * @file filename
 * @brief brief function description.
 * @details detailed function description.
 * @version 1.0
 * @author author_name
 * @date last_modified_date
 *
 *      Edit History
 *      ----------------------------------------------------------------------------
 *      DATE                     NAME               DESCRIPTION
 *      2024-08-2               jia fengyi         Create it.
 *
 */

package com.cart.musicdemo.fragment;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cart.musicdemo.R;
import com.cart.musicdemo.adapter.ArtistAdapter;
import com.cart.musicdemo.base.service.MusicService;
import com.cart.musicdemo.model.AlbumListModel;
import com.cart.musicdemo.model.SongModel;
import com.cart.musicdemo.model.UpdateEventUI;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ArtistFragment extends Fragment {

    private Map<String, List<SongModel>> artistList;

    private MusicService.MusicBinder musicService;

    private ArtistAdapter artistAdapter;

    //存放歌手名字list
    List<String> singerList;


    public ArtistFragment() {
        // Required empty public constructor
    }

    public static ArtistFragment newInstance(Map<String, List<SongModel>> singerList,
                                             MusicService.MusicBinder musicService) {
        ArtistFragment fragment = new ArtistFragment();
        Bundle args = new Bundle();
        AlbumListModel singerListModel = new AlbumListModel(singerList);
        args.putParcelable("singerList", singerListModel);
        args.putBinder("musicService", musicService);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        if (getArguments() != null) {
            AlbumListModel singerListModel = getArguments().getParcelable("singerList");
            artistList = singerListModel.getList();
            Log.d("artistList", "onCreate: " + artistList);
            musicService = (MusicService.MusicBinder) getArguments().getBinder("musicService");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUpdateEventUI(UpdateEventUI event) {
        String type = event.getType();
        if (type.equals("clearUI")) {
            //拔出USB时清空数据
            singerList.clear();
        }
        // 更新 UI
        artistAdapter.notifyDataSetChanged();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_artist_list, container, false);
        RecyclerView recyclerView = view.findViewById(R.id.rv_artist_RecyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        //获取Map当中的所有歌手、
        singerList = Collections.emptyList();
        // 使用Java 8的Stream API获取所有key并转换成List
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            singerList = artistList.keySet().stream()
                    .collect(Collectors.toList());
        }
        artistAdapter = new ArtistAdapter(singerList, this::onItemClick);
        recyclerView.setAdapter(artistAdapter);
        return view;
    }

    private void onItemClick(String singerName) {
        //跳转至歌手所拥有歌曲

        List<SongModel> list = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            list = artistList.getOrDefault(singerName, new ArrayList<>());
        }
        ArtistSongListFragment playListFragment = new ArtistSongListFragment(list, musicService);
        //fragment跳转+
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.artist_fragment, playListFragment);
        transaction.addToBackStack("artist");
        transaction.commit();

    }
}

